package com.elevator;

import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import data.setting;
interface liftUIEventListener
{
	public void liftUIEventProcess(int nEvent,Object object);
}
public class liftUI extends JPanel {
	/**
	 * 
	 */
	public JLabel mLabel = new JLabel();
	private static final long serialVersionUID = 1L;
	public JButton []mBtns = null;
	public liftUIEventListener mListener = null; 
	
	doorUI mDoorUI= new doorUI();
	public ActionListener mAction =new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			JButton btn = (JButton)e.getSource();
			if (liftUI.this.mListener != null) {
				int nIndex = 0;
				for (int i = 0; i < mBtns.length; i++) {
					if (mBtns[i] == btn) {
						nIndex = i;
					}
				}
				liftUI.this.mListener.liftUIEventProcess(nIndex, btn);
			}

		}
	};
	public int getCurLayer()
	{
		String strCurLayer = mLabel.getText();
		int nIndex = 0;
		if (!strCurLayer.equals("G")) {
			nIndex = Integer.parseInt(strCurLayer);
		}
		return nIndex;
	}
	public liftUI() {
		this.setSize(150, 150);
		this.setBackground(Color.gray);
		this.setLayout(null);
		
		mLabel.setSize(20, 20);
		mLabel.setLocation(70, 10);
		mLabel.setText("G");
		mLabel.setOpaque(true);
		mLabel.setBackground(Color.yellow);
		mLabel.setHorizontalAlignment(SwingConstants.CENTER);
		mLabel.setVerticalAlignment(SwingConstants.CENTER);
		this.add(mLabel);
		
		mBtns = new JButton[setting.sLayerCount];

		for (int i = 0; i < mBtns.length; i++) {
			mBtns[i] = new JButton();
			mBtns[i].setSize(20,20);
			if (i >0)
			{
				mBtns[i].setText(""+i);
			}
			else 
			{
				mBtns[i].setText("G");
			}
			
			mBtns[i].setMargin(new Insets(0, 0, 0, 0));
			mBtns[i].setLocation(100 + (i % 2)*21, 30 + i/2*21);
			mBtns[i].addActionListener(mAction);
			this.add(mBtns[i]);
		}
		
		
		this.add(mDoorUI);
		mDoorUI.setLocation(20, 50);
		//doorUI.startOpenDoor();
	}
}
